package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.RequisitionLine;
import com.zyd.shiro.business.enums.AssetsType;
import com.zyd.shiro.business.service.PakRequisitionLineService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakAssets;
import com.zyd.shiro.persistence.beans.PakRequisitionLine;
import com.zyd.shiro.persistence.mapper.PakAssetsMapper;
import com.zyd.shiro.persistence.mapper.PakRequisitionLineMapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class PakRequisitionLineServiceImpl implements PakRequisitionLineService {

    @Autowired
    private PakRequisitionLineMapper pakRequisitionLineMapper;

    @Autowired
    private PakAssetsMapper pakAssetsMapper;

    @Override
    public RequisitionLine insert(RequisitionLine requisitionLine) {
        Assert.notNull(requisitionLine, "对象不可为空！");
        requisitionLine.getPakRequisitionLine().setUpdateTime(new Date());
        requisitionLine.getPakRequisitionLine().setCreateTime(new Date());
        pakRequisitionLineMapper.insertSelective(requisitionLine.getPakRequisitionLine());

        //修改资产状态
        PakAssets pakAssets = pakAssetsMapper.selectByPrimaryKey(requisitionLine.getPakRequisitionLine().getAssets());
        pakAssets.setStatus(AssetsType.CLAIM.getCode());
        pakAssetsMapper.updateByPrimaryKeySelective(pakAssets);

        return requisitionLine;
    }

    @Override
    public void insertList(List<RequisitionLine> requisitionLines) {
            for(RequisitionLine requisitionLine:requisitionLines){
               requisitionLine.getPakRequisitionLine().setUpdateTime(new Date());
               requisitionLine.getPakRequisitionLine().setCreateTime(new Date());
               pakRequisitionLineMapper.insertSelective(requisitionLine.getPakRequisitionLine());
           }
    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakRequisitionLineMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(RequisitionLine requisitionLine) {
        Assert.notNull(requisitionLine, "对象不可为空！");
        requisitionLine.setUpdateTime(new Date());
        return pakRequisitionLineMapper.updateByPrimaryKey(requisitionLine.getPakRequisitionLine()) > 0;
    }

    @Override
    public boolean updateSelective(RequisitionLine requisitionLine) {
        Assert.notNull(requisitionLine, "对象不可为空！");
        requisitionLine.setUpdateTime(new Date());
        return pakRequisitionLineMapper.updateByPrimaryKeySelective(requisitionLine.getPakRequisitionLine()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public RequisitionLine getByPrimaryKey(Long primaryKey) {
        PakRequisitionLine pakRequisitionLine = pakRequisitionLineMapper.selectByPrimaryKey(primaryKey);
        return null == pakRequisitionLine ? null : new RequisitionLine(pakRequisitionLine);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public RequisitionLine getOneByEntity(RequisitionLine test) {
        Assert.notNull(test, "对象不可为空！");
        PakRequisitionLine pakRequisitionLine = pakRequisitionLineMapper.selectOne(test.getPakRequisitionLine());
        return null == pakRequisitionLine ? null : new RequisitionLine(pakRequisitionLine);
    }

    @Override
    public List<RequisitionLine> listAll() {
        List<PakRequisitionLine> pakRequisitionLines = pakRequisitionLineMapper.selectAll();
        if (CollectionUtils.isEmpty(pakRequisitionLines)) {
            return null;
        }
        List<RequisitionLine> requisitionLines = new ArrayList<>();
        for (PakRequisitionLine pakRequisitionLine : pakRequisitionLines) {
            requisitionLines.add(new RequisitionLine(pakRequisitionLine));
        }
        return requisitionLines;
    }

    @Override
    public List<RequisitionLine> listByEntity(RequisitionLine entity) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<RequisitionLine> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakRequisitionLine> pakRequisitionLines = pakRequisitionLineMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakRequisitionLines)) {
            return null;
        }
        List<RequisitionLine> requisitionLines = new ArrayList<>();
        for (PakRequisitionLine pakRequisitionLine : pakRequisitionLines) {
            requisitionLines.add(new RequisitionLine(pakRequisitionLine));
        }
        PageInfo bean = new PageInfo<PakRequisitionLine>(pakRequisitionLines);
        bean.setList(requisitionLines);
        return bean;
    }
@Override
    public void increase(PakRequisitionLine pakRequisitionLine) {
        pakRequisitionLineMapper.insert(pakRequisitionLine);
    }




}
